from xen.xend.XendConfig import XendConfig
from xen.xend.XendError import XendError, XendInvalidDomain, VmError
from xen.xend.XendLogging import log
+from xen.xend.XendAPIConstants import XEN_API_VM_POWER_STATE
from xen.xend.XendConstants import XS_VMROOT
-from xen.xend.XendConstants import DOM_STATE_HALTED, DOM_STATE_RUNNING
+from xen.xend.XendConstants import DOM_STATE_HALTED, DOM_STATE_PAUSED
+from xen.xend.XendConstants import DOM_STATE_RUNNING, DOM_STATE_SUSPENDED
+from xen.xend.XendConstants import DOM_STATE_SHUTDOWN, DOM_STATE_UNKNOWN
from xen.xend.XendDevices import XendDevices
from xen.xend.xenstore.xstransact import xstransact
DOM0_NAME = "Domain-0"
DOM0_ID = 0
+POWER_STATE_NAMES = dict([(x, XEN_API_VM_POWER_STATE[x])
+ for x in [DOM_STATE_HALTED,
+ DOM_STATE_PAUSED,
+ DOM_STATE_RUNNING,
+ DOM_STATE_SUSPENDED,
+ DOM_STATE_SHUTDOWN,
+ DOM_STATE_UNKNOWN]])
+POWER_STATE_ALL = 'all'
+
+
class XendDomain:
"""Index of all domains. Singleton.
# ------------------------------------------------------------
# Xen Legacy API
- def list(self):
+ def list(self, state = DOM_STATE_RUNNING):
"""Get list of domain objects.
+ @param: the state in which the VMs should be -- one of the
+ DOM_STATE_XYZ constants, or the corresponding name, or 'all'.
@return: domains
@rtype: list of XendDomainInfo
"""
+ if type(state) == int:
+ state = POWER_STATE_NAMES[state]
+ state = state.lower()
+
self.domains_lock.acquire()
try:
self._refresh()
if dom_uuid not in active_uuids:
inactive_domains.append(dom)
- return active_domains + inactive_domains
+ if state == POWER_STATE_ALL:
+ return active_domains + inactive_domains
+ else:
+ return filter(lambda x:
+ POWER_STATE_NAMES[x.state].lower() == state,
+ active_domains + inactive_domains)
finally:
self.domains_lock.release()
- def list_sorted(self):
+ def list_sorted(self, state = DOM_STATE_RUNNING):
"""Get list of domain objects, sorted by name.
+ @param: the state in which the VMs should be -- one of the
+ DOM_STATE_XYZ constants, or the corresponding name, or 'all'.
@return: domain objects
@rtype: list of XendDomainInfo
"""
- doms = self.list()
+ doms = self.list(state)
doms.sort(lambda x, y: cmp(x.getName(), y.getName()))
return doms
- def list_names(self):
+ def list_names(self, state = DOM_STATE_RUNNING):
"""Get list of domain names.
+ @param: the state in which the VMs should be -- one of the
+ DOM_STATE_XYZ constants, or the corresponding name, or 'all'.
@return: domain names
@rtype: list of strings.
"""
- return [d.getName() for d in self.list_sorted()]
+ return [d.getName() for d in self.list_sorted(state)]
def domain_suspend(self, domname):
"""Suspends a domain that is persistently managed by Xend
from xen.xend import XendAPI, XendDomain, XendDomainInfo, XendNode
from xen.xend import XendLogging, XendDmesg
from xen.xend.XendClient import XML_RPC_SOCKET
+from xen.xend.XendConstants import DOM_STATE_RUNNING
from xen.xend.XendLogging import log
from xen.xend.XendError import XendInvalidDomain
info = lookup(domid)
return fixup_sxpr(info.sxpr(not full))
-def domains(detail=1, full = 0):
- if detail < 1:
- return XendDomain.instance().list_names()
- else:
- domains = XendDomain.instance().list_sorted()
+def domains(detail = True, full = False):
+ return domains_with_state(detail, DOM_STATE_RUNNING, full)
+
+def domains_with_state(detail, state, full):
+ if detail:
+ domains = XendDomain.instance().list_sorted(state)
return map(lambda dom: fixup_sxpr(dom.sxpr(not full)), domains)
+ else:
+ return XendDomain.instance().list_names(state)
def domain_create(config):
info = XendDomain.instance().domain_create(config)
# A few special cases
self.server.register_function(domain, 'xend.domain')
self.server.register_function(domains, 'xend.domains')
+ self.server.register_function(domains_with_state,
+ 'xend.domains_with_state')
self.server.register_function(get_log, 'xend.node.log')
self.server.register_function(domain_create, 'xend.domain.create')
self.server.register_function(domain_restore, 'xend.domain.restore')
('-c CAP', '--cap=CAP', 'Cap (int)'),
),
'list': (
- ('-l', '--long', 'Output all VM details in SXP'),
- ('', '--label', 'Include security labels'),
+ ('-l', '--long', 'Output all VM details in SXP'),
+ ('', '--label', 'Include security labels'),
+ ('', '--state=<state>', 'Select only VMs with the specified state'),
),
'console': (
('-q', '--quiet', 'Do not print an error message if the domain does not exist'),
server.xend.domain.restore(savefile, paused)
-def getDomains(domain_names, full = 0):
+def getDomains(domain_names, state, full = 0):
if domain_names:
return [server.xend.domain(dom, full) for dom in domain_names]
else:
- return server.xend.domains(1, full)
+ return server.xend.domains_with_state(True, state, full)
def xm_list(args):
use_long = 0
show_vcpus = 0
show_labels = 0
+ state = 'all'
try:
(options, params) = getopt.gnu_getopt(args, 'lv',
- ['long','vcpus','label'])
+ ['long','vcpus','label',
+ 'state='])
except getopt.GetoptError, opterr:
err(opterr)
usage('list')
show_vcpus = 1
if k in ['--label']:
show_labels = 1
+ if k in ['--state']:
+ state = v
+
+ if state != 'all' and len(params) > 0:
+ raise OptionError(
+ "You may specify either a state or a particular VM, but not both")
if show_vcpus:
print >>sys.stderr, (
xm_vcpu_list(params)
return
- doms = getDomains(params, use_long)
+ doms = getDomains(params, state, use_long)
if use_long:
map(PrettyPrint.prettyprint, doms)
opts['weight'] = v
doms = filter(lambda x : domid_match(domid, x),
- [parse_doms_info(dom) for dom in getDomains("")])
+ [parse_doms_info(dom)
+ for dom in getDomains(None, 'running')])
# print header if we aren't setting any parameters
if len(opts.keys()) == 0:
if k in ['-s', '--short']:
short_mode = 1
- doms = getDomains(params)
+ doms = getDomains(params, 'running')
if short_mode == 0:
print 'Name ID Uptime'